package com.sense.kukoo.biz_explore.adapter.provider

import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.utils.TextUtils
import com.sense.kukoo.biz_explore.adapter.ExploreContentAdapter
import com.sense.kukoo.common.R
import com.sense.kukoo.common.repo.bean.ExploreContentBean
import com.sense.kukoo.common.wiget.EditorPicksItemView
import com.sense.kukoo.common.wiget.EditorPicksItemView2
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder

class EditorPicks2ItemProvider(val mListener: ExploreContentAdapter.ExploreContentEventListener) :
    MultiItemProvider<ExploreContentBean>() {

    override fun getItemType(): Int = ExploreContentBean.TYPE_3_X_2

    override fun getItemLayoutId(): Int = R.layout.common_editor_picks_2

    override fun onBindViewHolder(viewHolder: BaseViewHolder, bean: ExploreContentBean) {
        //
        val tv_see_all = viewHolder.getView(R.id.tv_see_all)
        if (bean.code==null) {
            tv_see_all.visibility = View.GONE
        }else{
            tv_see_all.visibility = View.VISIBLE
        }
        tv_see_all.setOnClickListener {
            mListener.onClickSeeAll(bean, bean.code!!)
        }
        //
        val tv_title = viewHolder.getView(R.id.tv_title) as TextView
        tv_title.text = bean.title
        //
        val items: Array<EditorPicksItemView2> = arrayOf(
                viewHolder.getView(R.id.item_1) as EditorPicksItemView2,
                viewHolder.getView(R.id.item_2) as EditorPicksItemView2,
                viewHolder.getView(R.id.item_3) as EditorPicksItemView2,
                viewHolder.getView(R.id.item_4) as EditorPicksItemView2,
                viewHolder.getView(R.id.item_5) as EditorPicksItemView2,
                viewHolder.getView(R.id.item_6) as EditorPicksItemView2
        )
        //
        val books = bean.books!!
        items.forEachIndexed{ index, view ->
            if(books.size > index){
                val bookBean = books[index]
                val name = if (TextUtils.isEmpty(bookBean.alias)) bookBean.name else bookBean.alias
                view.setDes(name)
                if(bookBean.isChapter == 1){
                    //单集
                    view.setIsMsgSubscribeType(false)
                    view.setMsg("${bookBean.listeners ?: 0}")
                }else{
                    view.setIsMsgSubscribeType(true)
                    view.setMsg("${bookBean.subscribers ?: 0}")
                }
                view.setIconUrl(bookBean.coverUrl)
                //点击事件
                view.setItemClickListener {
                    mListener.onClickBook(bean, bookBean)
                }
                view.visibility = View.VISIBLE
            }else{
                view.visibility = View.GONE
            }
        }
    }
}